<template>
  <d2-container>
    <page-header
      slot="header"
      :loading="loading"
      @submit="handleSubmit"
      ref="header"/>

    <page-main
      :table-data="table"
      :loading="loading"
      @sort="handleSort"
      @refresh="handleRefresh"/>

    <page-footer
      slot="footer"
      :loading="loading"
      :current="page.current"
      :size="page.size"
      :total="page.total"
      @change="handlePaginationChange"/>
  </d2-container>
</template>

<script>
import { getShowroomPage } from '@api/showroom/showroom'

export default {
  name: 'roomcollection-showroom-index',
  components: {
    'PageHeader': () => import('./components/PageHeader'),
    'PageMain': () => import('./components/PageMain'),
    'PageFooter': () => import('@/layout/header-aside/components/footer')
  },
  data () {
    return {
      loading: false,
      table: [],
      group: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      dynasty: 1,
      keyword: '',
      banners: []
    }
  },
  mounted: function () {
    this.handleSubmit()
  },
  methods: {
    //  新增管理员
    addAdminRequest: function () {
    },
    ceate: function () {
      this.$router.push({ name: 'information-banner-create', params: { } })
    },
    editRowWithNewTemplate () {
      this.$refs.d2Crud.showDialog({
        mode: 'edit',
        template: {
          date: {
            title: '日期',
            value: ''
          },
          name: {
            title: '姓名',
            value: ''
          }
        }
      })
    },
    addRowWithNewTemplate () {
      this.$refs.d2Crud.showDialog({
        mode: 'add',
        template: `
          <h1 style="color:red">我是选项模板</h1>
        `
      })
    },

    // 分页变化改动
    handlePaginationChange (val) {
      this.page = val
      this.$nextTick(() => {
        this.$refs.header.handleFormSubmit()
      })
    },

    // 删除操作
    handleDelete: function () {
      console.log(1, 2)
    },

    // 刷新列表页面
    handleRefresh (isTurning = false) {
      if (isTurning) {
        !(this.page.current - 1) || this.page.current--
      }

      this.$nextTick(() => {
        this.$refs.header.handleFormSubmit()
      })
    },

    // 排序刷新
    handleSort (val) {
      this.order = val
      this.$nextTick(() => {
        this.$refs.header.handleFormSubmit()
      })
    },

    // 提交查询请求
    handleSubmit (form, isRestore = false) {
      if (isRestore) {
        this.page.current = 1
      }

      this.loading = false
      getShowroomPage({
        dynasty: this.dynasty,
        keyword: this.keyword,
        pageSize: this.page.size,
        pageNum: this.page.current
      })
        .then(res => {
          this.table = res.showrooms || []
          this.page.total = res.totalCount
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
